package com.maps.view;

import java.util.List;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.graphics.drawable.Drawable;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.maps.websocket.WebSocketManager;
import com.maps.view.CustomItemizedOverlay;
import com.maps.view.R;

import de.roderick.weberknecht.WebSocketEventHandler;
import de.roderick.weberknecht.WebSocketMessage;

public class MapUnlockActivity extends MapActivity {
	private MapView mapView;
	private EditText txtSearch;
	private EditText txtSet;
	private static final int latitudeE6 = 31728167;
	private static final int longitudeE6 = 104172363;
	private boolean connectedFlag = false;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initComponent();
        connectWebsocket();
     // TODO Auto-generated method stub
		
        final Button btnSearch = (Button)findViewById(R.id.btnsearch);
        btnSearch.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (WebSocketManager.connectClosed){
					connectWebsocket();
				}
				String str2 = txtSearch.getText().toString();
				Log.d("DEBUG", "client send msg:" + str2);
				WebSocketManager.send(str2);
			}
		});
        
    	mapView = (MapView) findViewById(R.id.map_view);
    	mapView.setBuiltInZoomControls(true);
    	
    	GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
    	
    	Drawable drawable = this.getResources().getDrawable(R.drawable.bubble);
    	OverlayItem overlayitem =new OverlayItem(point, "Hello", "Im in Athens, Greece!");
    	CustomItemizedOverlay itemizedOverlay =new CustomItemizedOverlay(drawable, this);
    	itemizedOverlay.addOverlay(overlayitem);
    	
    	List<Overlay> mapOverlays = mapView.getOverlays();
    	mapOverlays.add(itemizedOverlay);
    	
    	MapController mapController = mapView.getController();
    	mapController.animateTo(point);
    	mapController.setZoom(6);
    }
    private void initComponent() {
    	txtSearch = (EditText) findViewById(R.id.txtSearch);
    	txtSet = (EditText) findViewById(R.id.editText1);
    }
    
    private void connectWebsocket(){
    	WebSocketManager.connect("ws://192.168.0.111:8000/", new WebSocketEventHandler() {
			public void onOpen() {
				Log.d("DEBUG", "client connect open");
				WebSocketManager.connectClosed = false;
			}
			public void onMessage(WebSocketMessage message) {
                String str = message.getText();
                Log.d("DEBUG", "client message on " + str);
                //txtSet.setText(str);
                //Intent intent = new Intent(getApplicationContext(), (Class)移動させたいActivity.class);
                //startActivity(intent);
      			//finish();
			}
			public void onClose() {
				WebSocketManager.connectClosed = true;
                Log.d("DEBUG", "client connect close");
			}
			
		});
    }
    @Override
    protected boolean isRouteDisplayed() {

    return false;

    }
    public void btnSeach_onclick(View view){
    // TODO	
    }
}